<?php

/**************************************
CUSTOM META FIELD
***************************************/

	//metaboxes
	add_action('add_meta_boxes', 'register_canon_cmb_pages');
	add_action ('save_post', 'update_canon_cmb_pages');

	function register_canon_cmb_pages () {
		add_meta_box('canon_cmb_pages','Kodax Page Settings', 'display_canon_cmb_pages','page','normal','high');
	}

	function display_canon_cmb_pages ($post) {

	/**************************************
	GET VALUES
	***************************************/

		//options
		$default_excerpt_len = 400;

		//all templates
		$result_cmb_header_bg_img = get_post_meta($post->ID, 'cmb_header_bg_img', true);

		//gallery specific
		$result_cmb_gallery_style = get_post_meta($post->ID, 'cmb_gallery_style', true);
		$result_cmb_gallery_click = get_post_meta($post->ID, 'cmb_gallery_click', true);
		$result_cmb_gallery_excerpt = get_post_meta($post->ID, 'cmb_gallery_excerpt', true);
		$result_cmb_gallery_cat_ID = get_post_meta($post->ID, 'cmb_gallery_cat_ID', true);

		//blog specific
		$result_cmb_kodax_blog = get_post_meta($post->ID, 'cmb_kodax_blog', true);

		//team specific
		$result_cmb_kodax_team_member_num = get_post_meta($post->ID, 'cmb_kodax_team_member_num', true);
		$result_cmb_kodax_team_member = get_post_meta($post->ID, 'cmb_kodax_team_member', true);
		$result_cmb_kodax_team_info = get_post_meta($post->ID, 'cmb_kodax_team_info', true);

		//faq specific
		$result_cmb_kodax_faq = get_post_meta($post->ID, 'cmb_kodax_faq', true);

		//pricing specific
		$result_cmb_kodax_pricing = get_post_meta($post->ID, 'cmb_kodax_pricing', true);

		//exist
		$result_cmb_exist = get_post_meta($post->ID, 'cmb_exist', true);

		//make sure (empty) arrays are defined as arrays
		if (empty($result_cmb_gallery_cat_ID)) $result_cmb_gallery_cat_ID = array();
		if (empty($result_cmb_kodax_blog)) $result_cmb_kodax_blog = array();
		if (empty($result_cmb_kodax_team_member)) $result_cmb_kodax_team_member = array();
		if (empty($result_cmb_kodax_team_info)) $result_cmb_kodax_team_info = array();
		if (empty($result_cmb_kodax_faq)) $result_cmb_kodax_faq = array();
		if (empty($result_cmb_kodax_pricing)) $result_cmb_kodax_pricing = array();

		//defaults
		if (empty($result_cmb_exist)) {

			//gallery specific
			$result_cmb_gallery_style 		= "1";
			$result_cmb_gallery_click 		= "lightbox";

			//blog_specific
			$result_cmb_kodax_blog			= array (
				'style'						=> 'default',
			);

			//team specific
			$result_cmb_kodax_team_member_num	= 4;
 			$result_cmb_kodax_team_member	= array(
 				'0' => array(
 					'show'					=> 'checked',
 					'image' 				=> '',
 					'name'					=> 'Ron Burgundy',
 					'title'					=> 'Anchorman',
 					'description'			=> 'Nulla vitae elit libero, a pharetra augue. Nullam id dolor id nibh ultri cies cula.',
 					'show_social_links'		=> 'checked',
 					'social_links'			=> array(
 						'0'						=> array('fa-facebook','https://www.facebook.com/pages/Theme-Canon/117307468468269'),
 						),
 					),
 				'1' => array(
 					'show'					=> 'checked',
 					'image' 				=> '',
 					'name'					=> 'Veronica Corningstone',
 					'title'					=> 'Anchor Woman',
 					'description'			=> 'Pharetra augu nullam id dolor id nibh ultricies vehicula ut idemin asti elit.',
 					'show_social_links'		=> 'checked',
 					'social_links'			=> array(
 						'0'						=> array('fa-facebook','https://www.facebook.com/pages/Theme-Canon/117307468468269'),
 						),
 					),
 				'2' => array(
 					'show'					=> 'checked',
 					'image' 				=> '',
 					'name'					=> 'Brick Chandlan',
 					'title'					=> 'Weather Man',
 					'description'			=> 'Vitae elit libero, ansom phar etra augue. Nullam id dolor id nibh ultri cies cula.',
 					'show_social_links'		=> 'checked',
 					'social_links'			=> array(
 						'0'						=> array('fa-facebook','https://www.facebook.com/pages/Theme-Canon/117307468468269'),
 						),
 					),
 				'3' => array(
 					'show'					=> 'checked',
 					'image' 				=> '',
 					'name'					=> 'Dorothy Mantooth',
 					'title'					=> 'Mother',
 					'description'			=> 'Shar petra augu nullam id dolor id nibh ultricies vehicula ut idemin asti.',
 					'show_social_links'		=> 'checked',
 					'social_links'			=> array(
 						'0'						=> array('fa-facebook','https://www.facebook.com/pages/Theme-Canon/117307468468269'),
 						),
 					),
 			);
			$result_cmb_kodax_team_info		= array(
				'show_more_team_info'		=> 'checked',
				'title'						=> 'More About Our Team',
				'text'						=> '<div class="half">
	<h4>Culture</h4>
	Nullam id dolor id nibh ultricies vehicula ut id elit. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Maecenas sed diam eget risus varius blandit sit amet non magna. Nullam id dolor id nibh ultricies vehicula ut id elit. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Maecenas sed diam eget risus varius blandit sit amet non magna.Donec sed odio dui. Aenean lacinia bibendum nulla sed consectetur. Etiam porta sem malesuada magna mollis euismod. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Donec ullamcorper nulla non metus auctor fringilla. 

</div>
<div class="half last">
	<h4>Team Past Times</h4>
	Cras justo odio, dapibus ac facilisis in, egestas eget quam. Sed posuere consectetur est at lobortis.
	<br><br>
	<ol class="graphs">
		<li class="per-80">
	Beer Pong <span>80%</span></li>
		<li class="per-60">
	Gaming <span>60%</span></li>
		<li class="per-40">
	Scabble <span>40%</span></li>
		<li class="per-90">
	Ultimate Frisby <span>90%</span></li>
	</ol>
</div>',
			);

			
			//faq specific
			$result_cmb_kodax_faq 			= array(
				'show_knowledge_base'		=> 'checked',
				'questions'					=> array(
					'0'							=> array(
						'question'					=> "If You Die, What Happens To Your Facebook?",
						'answer'					=> "Nullam id dolor id nibh ultricies vehicula ut id elit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec id elit non mi porta gravida at eget.",
					),
					'1'							=> array(
						'question'					=> "My Credit Card Is Stuck In My Computer?",
						'answer'					=> "Donec ullamcorper nulla non metus auctor fringilla. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Cras mattis consectetur purus sit amet fermentum. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.",
					),
					'2'							=> array(
						'question'					=> "Why Doesn’t The Earth Fall Down?",
						'answer'					=> "Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
					),
				),

				'show_cta'					=> 'checked',
				'cta'						=> '<h2>Purchase this theme today, no sign up or credit card needed, <a href=""><span>Get it now</span></a></h2>',

				'show_faq'					=> 'checked',
				'faq_section_title'			=> 'Frequent Questions',
				'faq'						=> '<div class="half">
	<h5>Can I upgrade anytime?</h5>
	<p>Nulla vitae elit libero, a pharetra augue. Vestibulum id ligula porta felis euismod semper.</p>
	
	<h5>What is the payment term?</h5>
	<p>Donec sed odio duiulla vitae elit libero, a pharetra augue. Vestibulum id ligula porta felis euismod semper.</p>
</div>

<div class="half last">
	<h5>Can transfer my account?</h5>
	<p>Cras mattis consectetur purus sit amet fermentum a pharetra augue. Vestibulum id ligula porta felis euismod semper.</p>
	
	<h5>How can I defer my plan?</h5>
	<p>Sed posuere consectetur est at lobortis. Donec sed odio dui. Nulla vitae elit libero, a pharetra augue. Vestibulum id ligula porta felis euismod semper.</p>
</div>',
			);


			//pricing specific
			$result_cmb_kodax_pricing 		= array(
				'num'						=> 3,
				'show_pricing'				=> 'checked',
				'tables'					=> array(
					'0'							=> array (
						'title'						=> 'Beginner',
						'price'						=> '$9',
						'interval'					=> 'p/m',
						'description'				=> '<p><em>Donec sed odio dui. Cum sociis natoque penatibus et magnis dis.</em></p>

<ul>
	<li>Monthly Payment</li>
	<li>No Support</li>
	<li>1 User</li>
	<li>24/7 Backups</li>
	<li>Cloud Hosted</li>
</ul>',
						'button_text'				=> 'Sign up now',
						'button_link'				=> 'http://www.themecanon.com',
					),
					'1'							=> array (
						'feature'					=> 'checked',
						'title'						=> 'Intermediate',
						'price'						=> '$22',
						'interval'					=> 'p/m',
						'description'				=> '<p><em>Donec sed odio dui. Cum sociis natoque penatibus et magnis dis.</em></p>

<ul>
	<li>Monthly Payment</li>
	<li>Online Support</li>
	<li>10 Users</li>
	<li>24/7 Backups</li>
	<li>Triple Redundancy</li>
	<li>Best Value</li>
</ul>',
						'button_text'				=> 'Sign up now',
						'button_link'				=> 'http://www.themecanon.com',
					),
					'2'							=> array (
						'title'						=> 'Expert',
						'price'						=> '$34',
						'interval'					=> 'p/m',
						'description'				=> '<p><em>Donec sed odio dui. Cum sociis natoque penatibus et magnis dis.</em></p>

<ul>
	<li>Annual Payment</li>
	<li>Phone Support</li>
	<li>Unlimited Users</li>
	<li>24/7 Backups</li>
	<li>Cloud Hosted</li>
</ul>',
						'button_text'				=> 'Sign up now',
						'button_link'				=> 'http://www.themecanon.com',
					),

				),

				'show_faq'					=> 'checked',
				'faq_section_title'			=> 'Frequent Questions',
				'faq'						=> '<div class="half">
	<h5>Can I upgrade anytime?</h5>
	<p>Nulla vitae elit libero, a pharetra augue. Vestibulum id ligula porta felis euismod semper.</p>
	
	<h5>What is the payment term?</h5>
	<p>Donec sed odio duiulla vitae elit libero, a pharetra augue. Vestibulum id ligula porta felis euismod semper.</p>
</div>

<div class="half last">
	<h5>Can transfer my account?</h5>
	<p>Cras mattis consectetur purus sit amet fermentum a pharetra augue. Vestibulum id ligula porta felis euismod semper.</p>
	
	<h5>How can I defer my plan?</h5>
	<p>Sed posuere consectetur est at lobortis. Donec sed odio dui. Nulla vitae elit libero, a pharetra augue. Vestibulum id ligula porta felis euismod semper.</p>
</div>',
			);


		}

	/**************************************
	DISPLAY CONTENT
	***************************************/

		?>

	<!-- 
	--------------------------------------------------------------------------
		ALL TEMPLATES
    -------------------------------------------------------------------------- 
	-->
			<div class="option_heading">
				<span><?php _e("General Settings", "loc_kodax_core_plugin"); ?></span>
			</div>

			<div class="option_item">
				<label for='cmb_header_bg_img'><?php _e("Header background image", "loc_kodax_core_plugin"); ?></label><br>
				<input type='text' id='cmb_header_bg_img' name='cmb_header_bg_img' class="cmb_input" value='<?php if (isset($result_cmb_header_bg_img)) echo $result_cmb_header_bg_img; ?>'>
				<input type="button" id="upload_button_cmb_header_bg_img" class="upload_button" value="Select image" /><br>
			</div>

	<!-- 
	--------------------------------------------------------------------------
		TEMPLATE SPECIFIC: DEFAULT EMPTY
    -------------------------------------------------------------------------- 
	-->

		<div class="option_item default_hidden option_template_specific 
						option_page-template-name-here
		">
			<i><?php _e("No additional page settings for this template type", "loc_kodax_core_plugin"); ?>.</i>
		</div>

	<!-- 
	--------------------------------------------------------------------------
		TEMPLATE SPECIFIC: GALLERY
    -------------------------------------------------------------------------- 
	-->

		<div class=" default_hidden option_template_specific option_page-gallery">

			<div class="option_heading">
				<span><?php _e("Gallery Settings", "loc_kodax_core_plugin"); ?></span>
			</div>

			<div class="option_item">

				<label for='cmb_gallery_style'><?php _e("Gallery Style", "loc_kodax_core_plugin"); ?></label><br>
				<select id="cmb_gallery_style" name="cmb_gallery_style"> 
		 			<option value="one" <?php if (isset($result_cmb_gallery_style)) {if ($result_cmb_gallery_style == "one") echo "selected='selected'";} ?>><?php _e("Gallery Style 1", "loc_kodax_core_plugin"); ?></option> 
		 			<option value="two" <?php if (isset($result_cmb_gallery_style)) {if ($result_cmb_gallery_style == "two") echo "selected='selected'";} ?>><?php _e("Gallery Style 2", "loc_kodax_core_plugin"); ?></option> 
		 			<option value="three" <?php if (isset($result_cmb_gallery_style)) {if ($result_cmb_gallery_style == "three") echo "selected='selected'";} ?>><?php _e("Gallery Style 3", "loc_kodax_core_plugin"); ?></option> 
				</select> 

			</div>

			<div class="option_item">

				<label for='cmb_gallery_click'><?php _e("Clicking image", "loc_kodax_core_plugin"); ?></label><br>
				<select id="cmb_gallery_click" name="cmb_gallery_click"> 
		 			<option value="lightbox" <?php if (isset($result_cmb_gallery_click)) {if ($result_cmb_gallery_click == "lightbox") echo "selected='selected'";} ?>><?php _e("Opens lightbox", "loc_kodax_core_plugin"); ?></option> 
		 			<option value="post" <?php if (isset($result_cmb_gallery_click)) {if ($result_cmb_gallery_click == "post") echo "selected='selected'";} ?>><?php _e("Opens post", "loc_kodax_core_plugin"); ?></option> 
				</select> 

			</div>

			<div class="option_item">
				<label for='cmb_gallery_excerpt'><?php _e("Excerpt", "loc_kodax_core_plugin"); ?></label><br>
				<textarea id='cmb_gallery_excerpt' name='cmb_gallery_excerpt' class='widefat' rows='5'><?php if (!empty($result_cmb_gallery_excerpt)) echo $result_cmb_gallery_excerpt; ?></textarea>
				<button type="button" name="button_generate_excerpt" id='button_generate_excerpt' class="button-secondary auto_generate" value="<?php echo mb_make_excerpt($post->post_content, $default_excerpt_len, true); ?>">Auto-generate</button>
				<span class="item_hint float_right">&lt;p class="lead"&gt;<?php _e("Use this tag for lead text", "loc_kodax_core_plugin"); ?>&lt;/p&gt;</span>
			</div>

			<div class="option_item">

				<label for='cmb_gallery_cat_ID'><?php _e("Categories to be displayed in gallery", "loc_kodax_core_plugin"); ?></label><br><br>

	     			<?php 
	     				$categories = get_categories(array(
	     					'orderby' => 'name',
	     					'order' => 'ASC'
	     				));

						$categories = array_values($categories);

						for ($i = 0; $i < count($categories); $i++) {  
						?>
							<input type="hidden" name="cmb_gallery_cat_ID[<?php echo $categories[$i]->cat_ID; ?>]" value="unchecked" />
							<input type="checkbox" id="cmb_gallery_cat_ID[<?php echo $categories[$i]->cat_ID; ?>]" name="cmb_gallery_cat_ID[<?php echo $categories[$i]->cat_ID; ?>]" class="checkbox" value="checked" <?php if (isset($result_cmb_gallery_cat_ID[$categories[$i]->cat_ID])) checked($result_cmb_gallery_cat_ID[$categories[$i]->cat_ID] == "checked"); ?>/> 
							<?php echo $categories[$i]->name; ?><br>
						<?php
						}

	     			 ?>

				</select> 

			</div>


		</div>

		<!-- 
		--------------------------------------------------------------------------
			TEMPLATE SPECIFIC: BLOG 
	    -------------------------------------------------------------------------- 
		-->

		<div class=" default_hidden option_template_specific option_page-blog">

			<div class="option_heading">
				<span><?php _e("Blog Settings", "loc_kodax_core_plugin"); ?></span>
			</div>

			<div class="option_item">

				<label for='cmb_kodax_blog[style]'><?php _e("Blog Style", "loc_kodax_core_plugin"); ?></label><br>
				<select id="cmb_kodax_blog[style]" name="cmb_kodax_blog[style]"> 
		 			<option value="default" <?php if (isset($result_cmb_kodax_blog["style"])) {if ($result_cmb_kodax_blog["style"] == "default") echo "selected='selected'";} ?>><?php _e("Site default", "loc_kodax_core_plugin"); ?></option> 
		 			<option value="full" <?php if (isset($result_cmb_kodax_blog["style"])) {if ($result_cmb_kodax_blog["style"] == "full") echo "selected='selected'";} ?>><?php _e("Blog full width", "loc_kodax_core_plugin"); ?></option> 
		 			<option value="neat" <?php if (isset($result_cmb_kodax_blog["style"])) {if ($result_cmb_kodax_blog["style"] == "neat") echo "selected='selected'";} ?>><?php _e("Blog full width neat", "loc_kodax_core_plugin"); ?></option> 
		 			<option value="sidebar" <?php if (isset($result_cmb_kodax_blog["style"])) {if ($result_cmb_kodax_blog["style"] == "sidebar") echo "selected='selected'";} ?>><?php _e("Blog with sidebar", "loc_kodax_core_plugin"); ?></option> 
				</select> 

			</div>

		</div>

		<!-- 
		--------------------------------------------------------------------------
			TEMPLATE SPECIFIC: TEAM
	    -------------------------------------------------------------------------- 
		-->

		<?php 

			if ($result_cmb_kodax_team_member_num == "") $result_cmb_kodax_team_member_num = 1;

		?>

		<div class=" default_hidden option_template_specific option_page-team">

			<div class="option_heading">
				<span><?php _e("Team Members General Settings", "loc_kodax_core_plugin"); ?></span>
			</div>

			<div class="option_item">

				<input 
					type='number' 
					id='cmb_kodax_team_member_num' 
					name='cmb_kodax_team_member_num' 
					min='1'
					max='120'
					style='width: 40px;'
					value='<?php if (isset($result_cmb_kodax_team_member_num)) echo esc_attr($result_cmb_kodax_team_member_num); ?>'
				>
				<?php _e("Number of team members", "loc_kodax_core_plugin"); ?> <i>(<?php _e("Remember to update for changes to take effect", "loc_kodax_core_plugin"); ?>)</i>

			</div>
			<?php 

				for ($m = 0; $m < $result_cmb_kodax_team_member_num; $m++) {  

				?>
					
					<div class="option_heading togglable">
						<span><?php _e("Team Member ", "loc_kodax_core_plugin"); ?><?php echo $m+1; ?></span>
					</div>

					<div class="option_content_container default_hidden">

						<div class="option_item">
							<input type='checkbox' id='cmb_kodax_team_member[<?php echo $m; ?>][show]' name='cmb_kodax_team_member[<?php echo $m; ?>][show]' value='checked' <?php checked(!empty($result_cmb_kodax_team_member[$m]["show"])); ?>>
							<label for='cmb_kodax_team_member[<?php echo $m; ?>][show]'><?php _e("Show this team member.", "loc_kodax_core_plugin"); ?></label>
						</div>

						<div class="option_item">
							<label for='cmb_kodax_team_member[<?php echo $m; ?>][image]'><?php _e("Team member image", "loc_kodax_core_plugin"); ?></label><br>
							<input type='text' id='cmb_kodax_team_member[<?php echo $m; ?>][image]' name='cmb_kodax_team_member[<?php echo $m; ?>][image]' class="cmb_input" value='<?php if (isset($result_cmb_kodax_team_member[$m]["image"])) echo $result_cmb_kodax_team_member[$m]["image"]; ?>'>
							<input type="button" id="upload_button_team_member_image" class="upload_button" value="Upload image" /><br>
						</div>

						<div class="option_item">
							<label for='cmb_kodax_team_member[<?php echo $m; ?>]["name"]'><?php _e("Name", "loc_kodax_core_plugin"); ?></label><br>
							<input type='text' id='cmb_kodax_team_member[<?php echo $m; ?>]["name"]' name='cmb_kodax_team_member[<?php echo $m; ?>][name]' class='widefat' value="<?php if (!empty($result_cmb_kodax_team_member[$m]["name"])) {echo htmlspecialchars($result_cmb_kodax_team_member[$m]["name"]);} ?>">
						</div>

						<div class="option_item">
							<label for='cmb_kodax_team_member[<?php echo $m; ?>]["title"]'><?php _e("Title", "loc_kodax_core_plugin"); ?></label><br>
							<input type='text' id='cmb_kodax_team_member[<?php echo $m; ?>]["title"]' name='cmb_kodax_team_member[<?php echo $m; ?>][title]' class='widefat' value="<?php if (!empty($result_cmb_kodax_team_member[$m]["title"])) {echo htmlspecialchars($result_cmb_kodax_team_member[$m]["title"]);} ?>">
						</div>

						<div class="option_item">
							<label for='cmb_kodax_team_member[<?php echo $m; ?>]["description"]'><?php _e("Description", "loc_kodax_core_plugin"); ?></label><br>
							<textarea id='cmb_kodax_team_member[<?php echo $m; ?>]["description"]' name='cmb_kodax_team_member[<?php echo $m; ?>][description]' class='widefat'><?php if (!empty($result_cmb_kodax_team_member[$m]["description"])) {echo $result_cmb_kodax_team_member[$m]["description"];} ?></textarea>
						</div>


					<!-- SOCIAL LINKS -->

						<div class="option_item">
						
							<input type='checkbox' id='cmb_kodax_team_member[<?php echo $m; ?>][show_social_links]' name='cmb_kodax_team_member[<?php echo $m; ?>][show_social_links]' value='checked' <?php checked(!empty($result_cmb_kodax_team_member[$m]["show_social_links"])); ?>>
							<label for='cmb_kodax_team_member[<?php echo $m; ?>][show_social_links]'><?php _e("Show social links:", "loc_kodax_core_plugin"); ?></label>

							<table class='form-table cmb_social_links'>
								<?php 

									$font_awesome_array = mb_get_font_awesome_icon_names_in_array();

									$social_links_num = (isset($result_cmb_kodax_team_member[$m]["social_links"])) ? count($result_cmb_kodax_team_member[$m]["social_links"]) : 1;

									for ($i = 0; $i < $social_links_num; $i++) {  
									?>

									<tr valign='top' class='cmb_social_links_row'>
										<th scope='row'>Social link <?php echo $i+1; ?></th>
										<td>
											<select class="cmb_social_links_icon" name='cmb_kodax_team_member[<?php echo $m; ?>][social_links][<?php echo $i; ?>][0]'> 
												<?php 

													for ($n = 0; $n < count($font_awesome_array); $n++) {  
													?>
								     					<option value="<?php echo $font_awesome_array[$n]; ?>" <?php if (isset($result_cmb_kodax_team_member[$m]["social_links"][$i][0])) {if ($result_cmb_kodax_team_member[$m]["social_links"][$i][0] == $font_awesome_array[$n]) echo "selected='selected'";} ?>><?php echo $font_awesome_array[$n]; ?></option> 
													<?php
													}

												?>
											</select> 

										<i class="fa <?php if (isset($result_cmb_kodax_team_member[$m]["social_links"][$i][0])) { echo $result_cmb_kodax_team_member[$m]["social_links"][$i][0]; } else { echo "fa fa-flag"; } ?>"></i>

										<input type='text' class='cmb_social_links_link' name='cmb_kodax_team_member[<?php echo $m; ?>][social_links][<?php echo $i; ?>][1]' value='<?php if (isset($result_cmb_kodax_team_member[$m]["social_links"][$i][1])) echo $result_cmb_kodax_team_member[$m]["social_links"][$i][1]; ?>'>
										</td>
									</tr>

									<?php

									}

								?>
							</table>

							<table class='form-table cmb_social_links_control'>
								<tr valign='top'>
									<th scope='row'></th>
									<td>
										<input type="button" class="button cmb_button_add_social_link" value="Add social link" />
										<input type="button" class="button cmb_button_remove_social_link" value="Remove social link" />
									</td>
								</tr>

							</table>
						</div>



					</div>
					<!-- end option_content_container -->


				<?php
				}

			?>

			<div class="option_heading togglable">
				<span><?php _e("More team info ", "loc_kodax_core_plugin"); ?></span>
			</div>

			<div class="option_content_container">
				
				<div class="option_item">
					<input type='checkbox' id='cmb_kodax_team_info[show_more_team_info]' name='cmb_kodax_team_info[show_more_team_info]' value='checked' <?php checked(!empty($result_cmb_kodax_team_info["show_more_team_info"])); ?>>
					<label for='cmb_kodax_team_info[show_more_team_info]'><?php _e("Show more team info.", "loc_kodax_core_plugin"); ?></label>
				</div>

				<div class="option_item">
					<label for='cmb_kodax_team_info[title]'><?php _e("Section title", "loc_kodax_core_plugin"); ?></label><br>
					<input type='text' id='cmb_kodax_team_info[title]' name='cmb_kodax_team_info[title]' class='widefat' value="<?php if (!empty($result_cmb_kodax_team_info["title"])) {echo htmlspecialchars($result_cmb_kodax_team_info["title"]);} ?>">
				</div>

				<div class="option_item">
					<?php 
						if (!isset($result_cmb_kodax_team_info["text"])) $result_cmb_kodax_team_info["text"] = "";

						wp_editor( $result_cmb_kodax_team_info["text"], 'cmb_kodax_team_info[text]', array(
						    'textarea_name' => 'cmb_kodax_team_info[text]',
						    'teeny' => false,
						    'media_buttons' => true,
						));
							
					?>
				</div>

			</div>

		</div>


		<!-- 
		--------------------------------------------------------------------------
			TEMPLATE SPECIFIC: FAQ
	    -------------------------------------------------------------------------- 
		-->

		<div class=" default_hidden option_template_specific option_page-faq">

			<!-- KNOWLEDGE BASE -->
			<div class="option_heading togglable">
				<span><?php _e("Knowledge Base", "loc_kodax_core_plugin"); ?></span>
			</div>

			<div class="option_content_container default_hidden">
				
				<div class="option_item">
					<input type='checkbox' id='cmb_kodax_faq[show_knowledge_base]' name='cmb_kodax_faq[show_knowledge_base]' value='checked' <?php checked(!empty($result_cmb_kodax_faq["show_knowledge_base"])); ?>>
					<label for='cmb_kodax_faq[show_knowledge_base]'><?php _e("Show knowledge base.", "loc_kodax_core_plugin"); ?></label>
				</div>

				<div class="question_container">

				<?php 
					$questions_num = (isset($result_cmb_kodax_faq["questions"])) ? count($result_cmb_kodax_faq["questions"]) : 1;
					for ($i = 0; $i < $questions_num; $i++) {  
					?>
						<div class="option_item">
							<label for='cmb_kodax_faq[questions][<?php echo $i; ?>][question]'><?php _e("Question", "loc_kodax_core_plugin"); ?> <b>#<?php echo $i+1; ?></b></label><br>
							<input type='text' id='cmb_kodax_faq[questions][<?php echo $i; ?>][question]' name='cmb_kodax_faq[questions][<?php echo $i; ?>][question]' class='widefat' value="<?php if (!empty($result_cmb_kodax_faq["questions"][$i]["question"])) {echo htmlspecialchars($result_cmb_kodax_faq["questions"][$i]["question"]);} ?>">
							<br><br>
							<label for='cmb_kodax_faq[questions][<?php echo $i; ?>][answer]'><?php _e("Answer", "loc_kodax_core_plugin"); ?></label><br>
							<textarea id='cmb_kodax_faq[questions][<?php echo $i; ?>][answer]' name='cmb_kodax_faq[questions][<?php echo $i; ?>][answer]' class='widefat'><?php if (!empty($result_cmb_kodax_faq["questions"][$i]["answer"])) {echo $result_cmb_kodax_faq["questions"][$i]["answer"];} ?></textarea>
						</div>
					<?php
					}

				?>

				</div>

				<div class="option_item">
					<input type="button" id="button_add_question" class="button" value="Add question"/>
					<input type="button" id="button_del_question" class="button" value="Remove question"/>
				</div>

			</div>

			<!-- CALL TO ACTION BOX -->
			<div class="option_heading togglable">
				<span><?php _e("Call to Action Box", "loc_kodax_core_plugin"); ?></span>
			</div>

			<div class="option_content_container default_hidden">
				
				<div class="option_item">
					<input type='checkbox' id='cmb_kodax_faq[show_cta]' name='cmb_kodax_faq[show_cta]' value='checked' <?php checked(!empty($result_cmb_kodax_faq["show_cta"])); ?>>
					<label for='cmb_kodax_faq[show_cta]'><?php _e("Show call to action box.", "loc_kodax_core_plugin"); ?></label>
				</div>

				<div class="option_item">
					<?php 

						if (!isset($result_cmb_kodax_faq["cta"])) $result_cmb_kodax_faq["cta"] = "";

						wp_editor( $result_cmb_kodax_faq["cta"], 'cmb_kodax_faq[cta]', array(
						    'textarea_name' => 'cmb_kodax_faq[cta]',
						    'teeny' => false,
						    'media_buttons' => true,
						    'textarea_rows' => 5,
						));
							
					?>
				</div>

			</div>

			<!-- FAQ -->
			<div class="option_heading togglable">
				<span><?php _e("FAQ", "loc_kodax_core_plugin"); ?></span>
			</div>

			<div class="option_content_container default_hidden">
				
				<div class="option_item">
					<input type='checkbox' id='cmb_kodax_faq[show_faq]' name='cmb_kodax_faq[show_faq]' value='checked' <?php checked(!empty($result_cmb_kodax_faq["show_faq"])); ?>>
					<label for='cmb_kodax_faq[show_faq]'><?php _e("Show FAQ.", "loc_kodax_core_plugin"); ?></label>
				</div>

				<div class="option_item">
					<label for='cmb_kodax_faq[faq_section_title]'><?php _e("Section title", "loc_kodax_core_plugin"); ?></label><br>
					<input type='text' id='cmb_kodax_faq[faq_section_title]' name='cmb_kodax_faq[faq_section_title]' class='widefat' value="<?php if (!empty($result_cmb_kodax_faq["faq_section_title"])) {echo htmlspecialchars($result_cmb_kodax_faq["faq_section_title"]);} ?>">
				</div>

				<div class="option_item">
					<?php 

						if (!isset($result_cmb_kodax_faq["faq"])) $result_cmb_kodax_faq["faq"] = "";

						wp_editor( $result_cmb_kodax_faq["faq"], 'cmb_kodax_faq[faq]', array(
						    'textarea_name' => 'cmb_kodax_faq[faq]',
						    'teeny' => false,
						    'media_buttons' => true,
						));
							
					?>
				</div>

			</div>

		</div>


		<!-- 
		--------------------------------------------------------------------------
			TEMPLATE SPECIFIC: PRICING
	    -------------------------------------------------------------------------- 
		-->

		<div class=" default_hidden option_template_specific option_page-pricing">

			<!-- PRICING TABLES -->
			<div class="option_heading togglable">
				<span><?php _e("Pricing tables", "loc_kodax_core_plugin"); ?></span>
			</div>

			<div class="option_content_container">

				<div class="option_item">
					<input type='checkbox' id='cmb_kodax_pricing[show_pricing]' name='cmb_kodax_pricing[show_pricing]' value='checked' <?php checked(!empty($result_cmb_kodax_pricing["show_pricing"])); ?>>
					<label for='cmb_kodax_pricing[show_pricing]'><?php _e("Show pricing tables", "loc_kodax_core_plugin"); ?></label>
				</div>

				<div class="option_item">

					<input 
						type='number' 
						id='cmb_kodax_pricing[num]' 
						name='cmb_kodax_pricing[num]' 
						min='1'
						max='5'
						style='width: 40px;'
						value='<?php if (isset($result_cmb_kodax_pricing["num"])) { echo esc_attr($result_cmb_kodax_pricing["num"]); } else { echo 1;} ?>'
					>
					<?php _e("Number of pricing tables", "loc_kodax_core_plugin"); ?> <i>(<?php _e("Remember to update for changes to take effect", "loc_kodax_core_plugin"); ?>)</i>

				</div>

				<?php 
					$pricing_num = isset($result_cmb_kodax_pricing["num"]) ? $result_cmb_kodax_pricing["num"] : 1;

					for ($i = 0; $i < $pricing_num; $i++) {  
					?>
						<div class="option_heading togglable">
							<span><?php _e("Pricing table ", "loc_kodax_core_plugin"); ?><?php echo $i+1; ?></span>
						</div>

						<div class="option_content_container default_hidden">

							<div class="option_item">
								<input type='checkbox' id='cmb_kodax_pricing[tables][<?php echo $i; ?>][feature]' name='cmb_kodax_pricing[tables][<?php echo $i; ?>][feature]' value='checked' <?php checked(!empty($result_cmb_kodax_pricing["tables"][$i]["feature"])); ?>>
								<label for='cmb_kodax_pricing[tables][<?php echo $i; ?>][feature]'><?php _e("Featured", "loc_kodax_core_plugin"); ?></label>
							</div>

							<div class="option_item">
								<label for='cmb_kodax_pricing[tables][<?php echo $i; ?>][title]'><?php _e("Title", "loc_kodax_core_plugin"); ?></label><br>
								<input type='text' id='cmb_kodax_pricing[tables][<?php echo $i; ?>][title]' name='cmb_kodax_pricing[tables][<?php echo $i; ?>][title]' class='widefat' value="<?php if (!empty($result_cmb_kodax_pricing["tables"][$i]["title"])) {echo htmlspecialchars($result_cmb_kodax_pricing["tables"][$i]["title"]);} ?>">
							</div>

							<div class="option_item">
								<label for='cmb_kodax_pricing[tables][<?php echo $i; ?>][price]'><?php _e("Price", "loc_kodax_core_plugin"); ?></label><br>
								<input type='text' id='cmb_kodax_pricing[tables][<?php echo $i; ?>][price]' name='cmb_kodax_pricing[tables][<?php echo $i; ?>][price]' class='widefat' value="<?php if (!empty($result_cmb_kodax_pricing["tables"][$i]["price"])) {echo htmlspecialchars($result_cmb_kodax_pricing["tables"][$i]["price"]);} ?>">
							</div>

							<div class="option_item">
								<label for='cmb_kodax_pricing[tables][<?php echo $i; ?>][interval]'><?php _e("Interval", "loc_kodax_core_plugin"); ?></label><br>
								<input type='text' id='cmb_kodax_pricing[tables][<?php echo $i; ?>][interval]' name='cmb_kodax_pricing[tables][<?php echo $i; ?>][interval]' class='widefat' value="<?php if (!empty($result_cmb_kodax_pricing["tables"][$i]["interval"])) {echo htmlspecialchars($result_cmb_kodax_pricing["tables"][$i]["interval"]);} ?>">
							</div>

							<div class="option_item">
								<?php 
									if (!isset($result_cmb_kodax_pricing["tables"][$i]["description"])) {
										$result_cmb_kodax_pricing["tables"][$i]["description"] = '<p><em>Donec sed odio dui. Cum sociis natoque penatibus et magnis dis.</em></p>

<ul>
	<li>Monthly Payment</li>
	<li>No Support</li>
	<li>1 User</li>
	<li>24/7 Backups</li>
	<li>Cloud Hosted</li>
</ul>';	
									}
									wp_editor( $result_cmb_kodax_pricing["tables"][$i]["description"], 'cmb_kodax_pricing[tables]['.$i.'][description]', array(
									    'textarea_name' => 'cmb_kodax_pricing[tables]['.$i.'][description]',
									    'teeny' => false,
									    'media_buttons' => true,
								    	'textarea_rows' => 10,
									));
										
								?>
							</div>

							<div class="option_item">
								<label for='cmb_kodax_pricing[tables][<?php echo $i; ?>][button_text]'><?php _e("Button text", "loc_kodax_core_plugin"); ?></label><br>
								<input type='text' id='cmb_kodax_pricing[tables][<?php echo $i; ?>][button_text]' name='cmb_kodax_pricing[tables][<?php echo $i; ?>][button_text]' class='widefat' value="<?php if (!empty($result_cmb_kodax_pricing["tables"][$i]["button_text"])) {echo htmlspecialchars($result_cmb_kodax_pricing["tables"][$i]["button_text"]);} ?>">
							</div>

							<div class="option_item">
								<label for='cmb_kodax_pricing[tables][<?php echo $i; ?>][button_link]'><?php _e("Button link", "loc_kodax_core_plugin"); ?></label><br>
								<input type='text' id='cmb_kodax_pricing[tables][<?php echo $i; ?>][button_link]' name='cmb_kodax_pricing[tables][<?php echo $i; ?>][button_link]' class='widefat' value="<?php if (!empty($result_cmb_kodax_pricing["tables"][$i]["button_link"])) {echo htmlspecialchars($result_cmb_kodax_pricing["tables"][$i]["button_link"]);} ?>">
							</div>


						</div>
						
					<?php
					}

				?>


			</div>

			<!-- FAQ -->
			<div class="option_heading togglable">
				<span><?php _e("FAQ", "loc_kodax_core_plugin"); ?></span>
			</div>

			<div class="option_content_container default_hidden">
				
				<div class="option_item">
					<input type='checkbox' id='cmb_kodax_pricing[show_faq]' name='cmb_kodax_pricing[show_faq]' value='checked' <?php checked(!empty($result_cmb_kodax_pricing["show_faq"])); ?>>
					<label for='cmb_kodax_pricing[show_faq]'><?php _e("Show FAQ.", "loc_kodax_core_plugin"); ?></label>
				</div>

				<div class="option_item">
					<label for='cmb_kodax_pricing[faq_section_title]'><?php _e("Section title", "loc_kodax_core_plugin"); ?></label><br>
					<input type='text' id='cmb_kodax_pricing[faq_section_title]' name='cmb_kodax_pricing[faq_section_title]' class='widefat' value="<?php if (!empty($result_cmb_kodax_pricing["faq_section_title"])) {echo htmlspecialchars($result_cmb_kodax_pricing["faq_section_title"]);} ?>">
				</div>

				<div class="option_item">
					<?php 

						if (!isset($result_cmb_kodax_pricing["faq"])) $result_cmb_kodax_pricing["faq"] = "";

						wp_editor( $result_cmb_kodax_pricing["faq"], 'cmb_kodax_pricing[faq]', array(
						    'textarea_name' => 'cmb_kodax_pricing[faq]',
						    'teeny' => false,
						    'media_buttons' => true,
						));
							
					?>
				</div>

			</div>

		</div>





		<!-- add nonce -->
		<input type="hidden" name="cmb_nonce" value="<?php echo wp_create_nonce(basename(__FILE__)); ?>" />
		<input type="hidden" name="cmb_exist" value="true" />
		<?php	
	}



/**************************************
UPDATE
***************************************/

	function update_canon_cmb_pages ($post_id) {
		// avoid activation on irrelevant admin pages
		if (!isset($_POST['cmb_nonce'])) {
			return false;		
		}

		// verify nonce.    
		if (!wp_verify_nonce($_POST['cmb_nonce'], basename(__FILE__)) || !isset($_POST['cmb_nonce'])) {
			return false;
		}

		if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
			return;
		} else {

			//all templates
			if (isset($_POST['cmb_header_bg_img'])) { update_post_meta($post_id, 'cmb_header_bg_img', $_POST['cmb_header_bg_img']); } else { update_post_meta($post_id, 'cmb_header_bg_img', null); };

			//gallery specific
			if (isset($_POST['cmb_gallery_style'])) { update_post_meta($post_id, 'cmb_gallery_style', $_POST['cmb_gallery_style']); } else { update_post_meta($post_id, 'cmb_gallery_style', null); };
			if (isset($_POST['cmb_gallery_click'])) { update_post_meta($post_id, 'cmb_gallery_click', $_POST['cmb_gallery_click']); } else { update_post_meta($post_id, 'cmb_gallery_click', null); };
			if (isset($_POST['cmb_gallery_excerpt'])) { update_post_meta($post_id, 'cmb_gallery_excerpt', $_POST['cmb_gallery_excerpt']); } else { update_post_meta($post_id, 'cmb_gallery_excerpt', null); };
			if (isset($_POST['cmb_gallery_cat_ID'])) { update_post_meta($post_id, 'cmb_gallery_cat_ID', $_POST['cmb_gallery_cat_ID']); } else { update_post_meta($post_id, 'cmb_gallery_cat_ID', null); };
			
			//blog specific
			if (isset($_POST['cmb_kodax_blog'])) { update_post_meta($post_id, 'cmb_kodax_blog', $_POST['cmb_kodax_blog']); } else { update_post_meta($post_id, 'cmb_kodax_blog', null); };

			//team specific
			if (isset($_POST['cmb_kodax_team_member_num'])) { update_post_meta($post_id, 'cmb_kodax_team_member_num', $_POST['cmb_kodax_team_member_num']); } else { update_post_meta($post_id, 'cmb_kodax_team_member_num', null); };
			if (isset($_POST['cmb_kodax_team_member'])) { update_post_meta($post_id, 'cmb_kodax_team_member', $_POST['cmb_kodax_team_member']); } else { update_post_meta($post_id, 'cmb_kodax_team_member', null); };
			if (isset($_POST['cmb_kodax_team_info'])) { update_post_meta($post_id, 'cmb_kodax_team_info', $_POST['cmb_kodax_team_info']); } else { update_post_meta($post_id, 'cmb_kodax_team_info', null); };

			//faq specific
			if (isset($_POST['cmb_kodax_faq'])) { update_post_meta($post_id, 'cmb_kodax_faq', $_POST['cmb_kodax_faq']); } else { update_post_meta($post_id, 'cmb_kodax_faq', null); };

			//pricing specific
			if (isset($_POST['cmb_kodax_pricing'])) { update_post_meta($post_id, 'cmb_kodax_pricing', $_POST['cmb_kodax_pricing']); } else { update_post_meta($post_id, 'cmb_kodax_pricing', null); };

			//exist
			if (isset($_POST['cmb_exist'])) { update_post_meta($post_id, 'cmb_exist', $_POST['cmb_exist']); } else { update_post_meta($post_id, 'cmb_exist', null); };

			// debug_function_to_force_halt_execution_on_postphp_to_view_non_fatal_events();
				
		}
	}


